﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DAL;
using BUS;
using Utility;

public partial class Admin_NhanVien : System.Web.UI.Page
{
    protected string CurrentColumnSort
    {
        get
        {
            return ViewState["CurrentColumnSort"].ToString();
        }
        set
        {
            ViewState["CurrentColumnSort"] = value;
        }
    }
    protected int STT
    {
        get
        {
            return int.Parse(ViewState["STT"].ToString());
        }
        set
        {
            ViewState["STT"] = value;
        }
    }
    protected string OrderType
    {
        get
        {
            return ViewState["OrderType"].ToString();
        }
        set
        {
            ViewState["OrderType"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.CurrentColumnSort = NhanVienColumn.TenDangNhap_NhanVien.ToString();
            this.OrderType = OrderDirection.ASC.ToString();
            LoadData();
        }
    }
 
    private void LoadData()
    {

        try
        {
            if (int.Parse(Session["MaPhanQuyen"].ToString()) == 0)
            {
                btn_Delete.Visible = true;
                btn_ThemNhanVien.Visible = true;
                btn_CapNhatTrangThai.Visible = true;
            }
            else
            {
                btn_Delete.Visible = false;
                btn_ThemNhanVien.Visible = false;
                btn_CapNhatTrangThai.Visible = false;
            }
        
        STT = (pager.CurrentIndex - 1) * pager.PageSize + 1;
        Criteria cre = new Criteria();
        int totalrecords = 0;
        
        pager.PageSize = 5;
        if (txt_tennsx.Text != " ")
            cre.add(Condition.AND, NhanVienColumn.TenNhanVien.ToString(), Condition.LIKE, txt_tennsx.Text);

        NhanVienCollection Collection = NhanVienBUS.GET(cre.Criter, pager.CurrentIndex, pager.PageSize, NhanVienColumn.TenDangNhap_NhanVien.ToString(), "ASC", out totalrecords);

        rpt_NhanVien.DataSource = Collection;
        rpt_NhanVien.DataBind();
        pager.ItemCount = totalrecords;
        if (pager.ItemCount <= 0 || pager.ItemCount <= pager.PageSize)
        {
            pager.Visible = false;
        }
        else
        {
            pager.Visible = true;
        }
       }
        catch
        {
            Response.Redirect("DangNhap.aspx");
        }

    }

    protected void rpt_NhanVien_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
       e.Item.ItemType == ListItemType.Item)
        {
            NhanVien NV = (NhanVien)e.Item.DataItem;

            Label lb_STT = (Label)e.Item.FindControl("lb_STT");
            lb_STT.Text = Convert.ToString(STT + e.Item.ItemIndex);

            HiddenField hf_ID = (HiddenField)e.Item.FindControl("hf_TenDangNhap");
            hf_ID.Value = NV.TenDangNhap_NhanVien.ToString();

            CheckBox chk = (CheckBox)e.Item.FindControl("cb_TrangThai");
            if (NV.TrangThai == 1)
                chk.Checked = true;
            else
                chk.Checked = false;

           
        }
    }

    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }

    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rpt_NhanVien.Items.Count; i++)
        {
            CheckBox cb = (CheckBox)rpt_NhanVien.Items[i].FindControl("cb_Delete");
            if (cb.Checked == true)
            {
                try
                {
                    HiddenField hf_ID = (HiddenField)rpt_NhanVien.Items[i].FindControl("hf_TenDangNhap");
                    string iD = hf_ID.Value;
                    NhanVienBUS.DELETE(iD);

                }
                catch { }
            }
        }
        LoadData();
    }
    
    protected void btn_CapNhatTrangThai_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rpt_NhanVien.Items.Count; i++)
        {
            RepeaterItem item = rpt_NhanVien.Items[i];
            CheckBox cb_TrangThai = (CheckBox)item.FindControl("cb_TrangThai");
            if (cb_TrangThai.Checked == true)
            {

                HiddenField hf_TenDangNhap = (HiddenField)item.FindControl("hf_TenDangNhap");
                NhanVienBUS.UpdateTrangThai(hf_TenDangNhap.Value, 1);
            }
            else
            {
                HiddenField hf_TenDangNhap = (HiddenField)item.FindControl("hf_TenDangNhap");
                NhanVienBUS.UpdateTrangThai(hf_TenDangNhap.Value, 0);
            }
        }
        LoadData();
    }

    protected void btn_timkiem_Click(object sender, EventArgs e)
    {
        LoadData();
    }

}